package com.itheima.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.beans.Transient;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    EmpExprService empExprService;
    @Override
    public PageResult<Emp> getPageResult(EmpQueryParam param) {
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());

        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(param.getName()), Emp::getName, param.getName());
        queryWrapper.eq(param.getGender() != null, Emp::getGender, param.getGender());
        queryWrapper.gt(param.getBegin() != null, Emp::getEntryDate, param.getBegin());
        queryWrapper.lt(param.getEnd() != null, Emp::getEntryDate, param.getEnd());

        // Page<Emp> pageInfo = getBaseMapper().selectPage(page, queryWrapper);
        //return new PageResult<>(pageInfo.getTotal(), pageInfo.getRecords());
        page(page, queryWrapper);
        List<Emp> empList = page.getRecords();

        Set<Integer> deptIds = empList.stream().map(Emp::getDeptId).collect(Collectors.toSet());
        List<Dept> depts = deptMapper.selectBatchIds(deptIds);
        Map<Integer, String> deptMap = depts.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));
        empList.forEach(e ->
                e.setDeptName(deptMap.get(e.getDeptId())));
        return new PageResult<>(page.getTotal(), empList);

    }
    @Transactional
    @Override
    public void saveEmp(Emp emp) {
        LocalDateTime now = LocalDateTime.now();
        emp.setUpdateTime(now);
        emp.setCreateTime(now);
        save(emp);

        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(e -> e.setEmpId(emp.getId()));
            empExprService.saveBatch(exprList);

        }
    }

    @Override
    public Emp getInfoById(Integer id) {
Emp emp = getById(id);

        Dept dept = deptMapper.selectById(emp.getDeptId());
if (dept != null){
    emp.setDeptName(dept.getName());
}

        List<EmpExpr> exprList = empExprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, id));
        emp.setExprList(exprList);
        return emp;
    }

    @Transactional
    @Override
    public void updateEmp(Emp emp) {
        updateById(emp);
//删除
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));
        //添加
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(e -> e.setEmpId(emp.getId()));
            empExprService.saveBatch(exprList);
        }
    }

    @Override
    public void getdelete(List<Integer> ids) {
        if (!CollectionUtils.isEmpty(ids)) {
            removeBatchByIds(ids);
            empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));
        }
    }
}
